<?php
/**
 */
class Yz_Action_SelectListByKey
    extends Yz_Action_SelectList
{

    /** the key name
     *  @var string
     */
    protected $keyName;

    /** the keys (list)
     *  @var int|array
     */
    protected $keys;


    /************************************
     *          Public methods          *
     ************************************/


    /**
     *  @param int $limit >= 0
     *  @return this
     */
    public function setKey( $keyName, array $keys )
    {
        debug();
        assert('is_string($keyName) and "" !== $keyName');
        $this->keys = $keys;
        $this->keyName = $keyName;
        $this->count = null;
        return $this;
    }



    /************************************
     *        Protected methods         *
     ************************************/

    /** Verify any arguments before building query
     *      verify the keys
     *  @return this
     */
    protected function verifyArguments()
    {
        debug();
        parent::verifyArguments();
        if (! $this->keyName)
            throw StateError::_new_();
        return $this;
    }


    /**
     *  Apply the arguments to the SQL statement
     *      Setup limit to avoid occasional selection of all rows
     *  @param Yz_SQL_Statement $sql
     *  @return this
     */
    protected function applyArguments(Yz_SQL_Statement $sql)
    {
        debug();
        parent::applyArguments($sql);
        $key = $this->keyName;
        $sql->where("{$key} IN (" . join(',', $this->keys) . ')');
        return $this;
    }


}
